local skel = fk.CreateSkill {
  name = "emo__yiqing",
}

Fk:loadTranslationTable{
  ["emo__yiqing"] = "移情",
  [":emo__yiqing"] = "你可以将一张牌交给其他角色视为对你或其使用【桃】。此牌在任意角色手牌时明置，你不可交出同类型的牌。",

  ["#emo__yiqing"] = "移情：交出一张牌视为使用【桃】",
  ["@@emo__yiqing"] = "移情",
  ["#emo__yiqing-give"] = "移情：将 %arg 交给一名其他角色",
}

local getTypes = function ()
  local types = {Card.TypeBasic, Card.TypeEquip, Card.TypeTrick}
  for _, p in ipairs(Fk:currentRoom().alive_players) do
    for _, id in ipairs(p.player_cards[Player.Hand]) do
      local c = Fk:getCardById(id)
      if c:getMark("@@emo__yiqing") ~= 0 then
        table.removeOne(types, c.type)
        if #types == 0 then break end
      end
    end
  end
  return types
end

skel:addEffect("viewas", {
  anim_type = "support",
  prompt = "#emo__yiqing",
  mute_card = false,
  pattern = "peach|.|nosuit",
  card_num = 1,
  card_filter = function (self, player, to_select, selected)
    if #selected == 0 then
      return table.contains(getTypes(), Fk:getCardById(to_select).type)
    end
  end,
  view_as = function (self, player, cards)
    if #cards ~= 1 then return nil end
    local c = Fk:cloneCard("peach")
    c.skillName = skel.name
    c:addFakeSubcard(cards[1])
    return c
  end,
  enabled_at_play = function (self, player)
    return #getTypes() > 0 and table.find(Fk:currentRoom().alive_players, function (p)
      return p ~= player
    end)
  end,
  enabled_at_response = function (self, player, response)
    return not response and table.find(Fk:currentRoom().alive_players, function (p)
      return p ~= player
    end)
  end,
})

skel:addEffect(fk.CardUsing, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return target == player and table.contains(data.card.skillNames, skel.name) and data.tos and #data.tos > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cid = data.card.fake_subcards[1]
    if cid == nil or not table.contains(player:getCardIds("he"), cid) then return end
    local others = room:getOtherPlayers(player, false)
    if #others == 0 then return end
    local targets = table.filter(data.tos, function (p)
      return p ~= player and not p.dead
    end)
    if #targets ~= 1 then
      targets = room:askToChoosePlayers(player, {
        targets = #targets > 1 and targets or others,
        min_num = 1, max_num = 1, skill_name = skel.name, cancelable = false,
        prompt = "#emo__yiqing-give:::" .. Fk:getCardById(cid):toLogString() ,
      })
    end
    room:obtainCard(targets[1], cid, true, fk.ReasonGive, player, skel.name, "@@emo__yiqing")
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    return player.seat == 1
  end,
  on_use = function(self, event, target, player, data)
    for _, move in ipairs(data) do
      if move.toArea ~= Player.Hand then
        for _, info in ipairs(move.moveInfo) do
          if Fk:getCardById(info.cardId):getMark("@@emo__yiqing") ~= 0 then
            player.room:setCardMark(Fk:getCardById(info.cardId), "@@emo__yiqing", 0)
          end
        end
      end
    end
  end,
})

skel:addEffect("visibility", {
  card_visible = function (self, player, card)
    if card:getMark("@@emo__yiqing") ~= 0 then
      return true
    end
  end,
})

return skel
